package com.cxh.rmsq.ui.activity.self

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.cxh.rmsq.R
import com.cxh.rmsq.databinding.ActivityLookMeBinding
import com.cxh.rmsq.databinding.ActivityMyBillListBinding
import com.cxh.rmsq.im.IMManager
import com.cxh.rmsq.net.newNet.ProjectViewModel
import com.cxh.rmsq.net.newNet.VipViewModel
import com.cxh.rmsq.ui.activity.TitleBaseActivity
import com.cxh.rmsq.ui.adapter.self.LookMeListAdapter
import com.cxh.rmsq.ui.adapter.self.MyBillListAdapter
import com.cxh.rmsq.utils.ToastUtils
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class MyBillListActivity : TitleBaseActivity() {
    private lateinit var binding: ActivityMyBillListBinding
    private var type = 0 //1订阅2粉丝
    private var listAdapter: MyBillListAdapter? = null
    private var page = 1
    private val viewModel by lazy {
        ViewModelProvider.NewInstanceFactory().create(VipViewModel::class.java)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMyBillListBinding.inflate(layoutInflater)
        setContentView(binding.root)

        titleBar.tvTitle.text = "我的账单"
        initView()
    }

    fun initView() {
        binding.rvList.setLayoutManager(LinearLayoutManager(this))
        listAdapter = MyBillListAdapter()
        listAdapter!!.setEmptyView(View.inflate(this, R.layout.empty_data_layout, null))

        binding.rvList.setAdapter(listAdapter)


        binding.refreshList.resetNoMoreData()
        binding.refreshList.setRefreshHeader(MaterialHeader(this))
        binding.refreshList.setRefreshFooter(ClassicsFooter(this))
        binding.refreshList.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                getData()
                refreshLayout.finishLoadMore()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                listAdapter!!.setNewInstance(null)
                getData()
            }
        })
    }

    override fun onResume() {
        super.onResume()
        page = 1
        listAdapter!!.setNewInstance(null)
        getData()
    }

    private fun getData() {
            val paramsMap = HashMap<String, Any>()
            paramsMap["pageNo"] = page
            paramsMap["pageSize"] = 10
            viewModel.getMyPayList(page,10) {
                if (binding.refreshList.isRefreshing()) {
                    binding.refreshList.finishRefresh()
                }
                if (it.data != null) {
                    if (page == 1) {
                        listAdapter!!.setNewInstance(it.data.list)
                        if (it.data.list!!.size < 10) {
                            binding.refreshList.setEnableLoadMore(false)
                        } else {
                            binding.refreshList.setEnableLoadMore(true)
                        }
                    } else {
                        if (it.data.list!!.size > 0) {
                            listAdapter!!.addData(it.data.list!!)
                            binding.refreshList.setEnableLoadMore(true)
                        } else {
                            binding.refreshList.setEnableLoadMore(false)
                            binding.refreshList.finishLoadMoreWithNoMoreData()
                        }
                    }
                    listAdapter!!.notifyDataSetChanged()
                }
            }
    }
}